﻿<html>
<head>
	<meta charset="utf-8">
	<title>认识颜色</title>
</head>
<body><div id="canvas">loading</div></body>
<script src="../org/unify.js"></script>
<script src="../org/lufylegend-1.10.1.min.js"></script>
<script>
	LInit(100,"canvas",640, 960, main);
	var btnw=300;
	var colors=["#FFFFFF","#000000","#FF0000","#00FF00","#0000FF","#FF66FF","#9900CC","#FF9900"];
	var names=["白","黑","红","绿","蓝","粉","紫","橙"];
	var values=[0,1,2,3,4,5,6,7];
	var btns=[];
	var title;
	var index=0;
	var path="../";
	var loadData = [
		{name:"pass",path:path+"sound/pass.mp3"},
		{name:"warn",path:path+"sound/warn.wav"}
	];
	function main () {
		Unify.setStage();

		loadingLayer = new LoadingSample5();
		addChild(loadingLayer);
		LLoadManage.load(loadData,function(progress){loadingLayer.setProgress(progress);},loadComplete);
	}
	function loadComplete(result){
		removeChild(loadingLayer);
		loadingLayer = null;
		Unify.setRightWrongSound(result);
		//Unify.backgroundColor("#999999")

		initGame();
	}
	function initGame(){
		addChild(Unify.createBitmap("#999999",0,0,LGlobal.width,LGlobal.height))
		initTitle("教宝宝认识颜色");
		title=Unify.createText(40,"0X000000","",0,80);
		this.addChild(title);
		createColor();
	}
	function initTitle(value){
		addChild(Unify.createText(50,"#000000",value,0,0));
	}
	function removeColor(){
		for(var i=0;i<btns.length;i++){
			removeChild(btns[i]);
		}
		btns.length=0;
	}
	function createColor(){
		removeColor();
		var btnColor;
		values=Unify.getRandomArrayByArray(values); 
		var temp=Math.floor(Math.random()*4);
		index=values[temp];
		for(var i=0;i<4;i++){
			var color=colors[values[i]];
			//trace(values[i]+"="+index)
			btnColor=Unify.createButton(colorBox(color),colorBox(color),colorBox(color));
			btnColor.data=values[i];
			btnColor.addEventListener(LMouseEvent.MOUSE_DOWN,onClick);
			addChild(btnColor);
			btns.push(btnColor);
		}
		//trace("-------------------")
		Unify.autoPosition(btns,10,200,2,btnw+10,btnw+10);
		title.text="选出"+names[index]+"色方块"
	}
	function colorBox(value){
		return Unify.createBitmap(value,0,0,btnw,btnw)
	}
	function onClick(event){
		var btn=event.currentTarget;
		if(btn.data==index){
			Unify.playSoundRight();
			createColor();
		}else{
			Unify.playSoundWrong();
		}
	}

</script>
</html>
